package com.coderbean.demo.util;


import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkUtil {
	
	public static final Logger logger = LoggerFactory.getLogger(FreemarkUtil.class);

	public static String parseTpl(String viewName, Map<String, Object> params) {
		Configuration cfg = SpringContextUtils.getBean(Configuration.class);
		String html = null;
		Template t = null;
		try {
			t = cfg.getTemplate(viewName + ".ftl");
			html = FreeMarkerTemplateUtils.processTemplateIntoString(t, params);
		} catch (IOException | TemplateException e) {
			logger.error(e.getMessage(), e);
		}
		return html;
	}

}
